<?php

namespace app\models;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "bf_bank".
 *
 * @property int $id
 * @property int|null $user_id 持有人ID
 * @property string|null $card 银行卡号
 * @property float|null $credit_limit 授信额度
 * @property string|null $binding_time 绑卡时间
 * @property string|null $end_time 截止时间
 * @property float|null $total_money 累计代付金额
 * @property int|null $df_date 代付天数
 * @property float|null $total_income 累计收益
 * @property int|null $status 状态1正常2冻结3已过期
 * @property string|null $bank_name 开户行
 * @property string|null $site 所在地区
 * @property string|null $branch 分行/支行
 * @property int|null $phone 手机号
 */
class Bank
{
    //授信列表
    public function credit($get)
    {
        if(!empty($get['user_id'])){
            $tjsql="select SUM(credit_limit) as credit_limit From bf_bank where user_id =".$get['user_id']. " AND status =1";
            $total=Yii::$app->db->createCommand($tjsql)->queryOne();
            $listsql="select a.card,a.credit_limit,b.bank_name,b.logo From bf_bank a LEFT JOIN bf_bank_info b ON b.bank_code = ".$get['bank_code']." where a.user_id=".$get['user_id'];
            $list=Yii::$app->db->createCommand($listsql)->queryAll();
            $date=[
                'total'=>$total,
                'list'=>$list
            ];
            return ['code' => 200, 'message' => 'OK', 'data' => $date];
        }else{
            return ['code' => 401, 'message' => 'OK', 'data' => ''];
        }

    }

    //绑定银行卡
    public function bindcard($post)
    {
        if(!empty($post)) {
            $post['binding_time'] = date("Y-m-d H:i:s");
            $post['status'] = 1;
            $insert = Yii::$app->db->createCommand()->insert('bf_bank', $post)->execute();
            return ['code' => 200, 'message' => 'OK', 'data' => $insert];
        }else{
            return ['code' => 401, 'message' => '缺少参数', 'data' => ''];
        }
    }
    //查看银行卡信息
    public function cardcheck($get)
    {
        if(!empty($get)){
            $listsql="select a.id,a.user_name,a.card,a.bank_name,a.site,a.branch,a.credit_limit,a.end_time From bf_bank a LEFT JOIN bf_bank_info b ON b.bank_code = ".$get['bank_code']." where a.id=".$get['id'];
            $cardres=Yii::$app->db->createCommand($listsql)->queryOne();
            return ['code' => 200, 'message' => 'OK', 'data' => $cardres];

        }else{
            return ['code' => 401, 'message' => '缺少参数', 'data' => ''];
        }
    }

    //修改银行卡信息
    public function cardupdate($post)
    {
        if(!empty($post)){
            //如果有该银行卡关联的代付信息，只能修改高于当前的授信额度
            $bank=Yii::$app->db2->createCommand('select a.total_money a.credit_limit From bf_bank a where id='.$post['id'])->queryOne();
            $newarray=[
                'user_name' => $post['user_name'] ,
                'card'=>$post['card']
            ];
        }
    }


}
